<?php
	function getSenderAddress()
	{
		$d = str_ireplace('_sharp_', '#', get('address'));
		return str_ireplace('+', ' ', $d);
	}
	function getSenderName()
	{
		$tables = array();
		$tables[0] = 'ex_shoppers';
		
		$fields = array();
		$fields[0] = 'name';
		
		$whereClause = 'memberID="'.$_POST['h53209'].'"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		$returns = explode(', ', $data['name'][0]);
		
		return $returns[1];
	}
	function getSender()
	{
		$tables = array();
		$tables[0] = 'ex_shoppers';
		
		$fields = array();
		$fields[0] = 'email';
		
		$whereClause = 'memberID="'.$_POST['h53209'].'"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		return $data['email'][0];
	}
	function getShippingName()
	{
		$tables = array();
		$tables[0] = 'ex_shipping';
		
		$fields = array();
		$fields[0] = 'shipName';
		
		$whereClause = 'shipID = "'.$_POST['a38078'][0].'"';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		if($data['queryStatus'] == 'DB Query success!')
		{
			return 'Shipping '.$data['shipName'][0];
		}
		
		return '#7 Makaturing St. cor. Banawe, Quezon City';
		
	}
	
	function postVars()
	{
		if(isset($_POST['z92425'][0]) && $_POST['z92425'][0] != '')
		{
			return $_POST['z92425'];
		}
		else
		{
			return 'null';
		}
	}
	function shipCost()
	{
		if(isset($_POST['e46750']) && $_POST['e46750'] != '')
		{
			return $_POST['e46750'];
		}
		else
		{
			return 'null';
		}
	}
	function getItemDetails($dataSession, $i)
	{	
		/*
			[0] - id
			[1] - category: m/w/a
			[2] - quantity
			[3] - size
		*/
		
		$values = explode('|', $dataSession);
		
		$mainCategory = array();		
		$mainCategory['m'] = 'men';
		$mainCategory['f'] = 'women';
		$mainCategory['a'] = 'accessories';
		
		$imgDirec = 'img/categories/'.$mainCategory;
		
		$tables = array();
		$tables[0] = 'ex_gallery_'.$values[1];
		
		$fields = array();
		$fields[0] = 'itemName';
		$fields[1] = 'itemDesc';
		$fields[2] = 'itemImg1';
		$fields[3] = 'itemPrice';
		$fields[4] = 'active';
		$fields[5] = 'itemSubCat';
		
		$whereClause = 'itemID = "'.$values[0].'" AND active > 0';
		
		$data = SQLRecover($tables, $fields, $whereClause);
		
		if($data['queryStatus'] == 'DB Query success!')
		{
			$price = ($data['active'][0] > 1 ? $data['itemPrice'][0] * (1 - ($data['active'][0]/100)) : $data['itemPrice'][0]);
			

			
			$ttl = $price * $values[2];
				$x = $i % 2;
				$imgDirec = '<img src="img/categories/'.$mainCategory[$values[1]].'/'.$data['itemSubCat'][0].'/'.$data['itemImg1'][0].'" width = "120px"/>';
				$return['priceTotal'] = $ttl;
				$return['realQty'] = ($values[1] == 'a' ? 0 : $values[2]);
				$return['accQty'] = ($values[1] == 'a' ? 1 : 0);	
				
				$return['table'] = '
				<tr>
					<td class="nameImg leftAlign">
						'.$imgDirec.'
					</td>
					<td class="name leftAlign'.($x == 1 ? '' : ' stripe').'">
						'.$data['itemName'][0].'
					</td>
					<td class="size centerAlign'.($x == 1 ? '' : ' stripe').'">
						'.$values[3].'
					</td>
					<td class="price centerAlign'.($x == 1 ? '' : ' stripe').'">
						P'.number_format($price, 2, '.', ',').'
					</td>
					<td class="qty centerAlign'.($x == 1 ? '' : ' stripe').'">
						'.$values[2].' '.($values[1] == 'a' ? 'pcs' : 'pairs').'
					</td>
					<td class="sTotalP rightAlign">
						PHP
					</td>
					<td class="sTotal rightAlign'.($x == 1 ? '' : ' stripe').'">
						'.number_format($ttl, 2, '.', ',').'
					</td>
				</tr>
				';
		}	
		else
		{
			$return['priceTotal'] = 'null';
			$return['table'] = 'null';
			$return['accQty'] = 'null';
			$return['realQty'] = 'null';
		}	
		
		return $return;
	}
	
	function displayTable($tableF, $ttlSubQty, $shipQty, $gTotal, $sender)
	{
		return '
		<table>
					<tr>
						<td colspan="7" class="orderHeader">
							ORDER SUMMARY
						</td>
					</tr>
					<th>Item Description</th>
					<th>&nbsp;</th>
					<th class="centerAlign">Size</th>
					<th>Price</th>
					<th class="centerAlign">Qty.</th>
					<th colspan="2" class="centerAlign">Subtotal</th>
					'.$tableF.'
					<tr class="bortop">
						<td colspan="4" class="leftAlign sizeDwn" id="shipIndicator" alt="'.(getShippingName() == '#7 Makaturing St. cor. Banawe, Quezon City' ? 'pickup' : 'nopickup').'">
							SHIPPING FEE - '.getShippingName().': '.$sender.' 
						</td>
						<td colspan="2" class="rightAlign sTotalP">
							PHP
						</td>
						<td class="rightAlign sTotal">
							'.number_format($shipQty, 2, '.', ',').'
						</td>				
					</tr>
					<tr class="bgB">
						<td colspan="3" class="stripe">
							
						</td>
						<td colspan="2" class="rightAlign stripe">
							<b>Order Total:</b>
						</td>
						<td class="rightAlign sTotalP">
							<b>PHP</b>
						</td>
						<td class="stripe rightAlign sTotal">
							<b>'.number_format($gTotal, 2, '.', ',').'</b>
						</td>
					</tr>
					</table>
		';
	}
	
	function saveToOrdersList($detailsHTML, $memID)
	{
		if(isset($_SESSION['cart']))
		{
			$tableName = 'ex_pending_orders';
			
			$fields = array();
			$fields[0] = 'orderDetails';
			$fields[1] = 'memberID';
			$fields[2] = 'expiry';
			$fields[3] = 'hash';
			$fields[4] = 'imgFile';
			
			$values = array();
			$values[0] = addslashes($detailsHTML);
			$values[1] = $memID;
			$values[2] = sqlDate('3 days');
			$values[3] = dechex(rand(10000, 99999));
			$values[4] = 'noImg.jpg';
			
			$data = SQLInsert($tableName, $fields, $values);
			
			$tables = array();
			$tables[0] = 'ex_pending_orders';
			
			$fields = array();
			$fields[0] = 'orderID';
			$fields[1] = 'hash';
			
			$whereClause = 'orderID > 0 ORDER BY orderID DESC';
			
			$data = SQLRecover($tables, $fields, $whereClause);

			return $data['hash'][0].'zdghd'.$data['orderID'][0];
		}
		
		return 'null';
	}
	
	function displayPage()
	{
		if(isset($_SESSION['shopper']))
		{
			$orderSessions = postVars();
			$tableF = '';
			$realQty = 0;
			$accQty = 0;
			$ttlSubQty = 0;
			for ($i=0; $i<sizeOf($orderSessions); $i++)
			{
				$dataG = getItemDetails($orderSessions[$i], $i);
				$tableF .= $dataG['table'];
				$realQty += $dataG['realQty'];
				$accQty += $dataG['accQty'];
				$ttlSubQty += $dataG['priceTotal'];
			}
			$accQty = ($accQty >= 1 ? 1 : 0);
			$shipQty = shipCost()*($accQty + $realQty);
			$gTotal = $shipQty + $ttlSubQty;
			
			
			$senderVars = ''.(getSenderAddress() == 'pickup' ? '' : getSenderAddress()).'<br />';
			
			
			$tabledForm = displayTable($tableF, $ttlSubQty, $shipQty, $gTotal, $senderVars);
			$serverRecord = saveToOrdersList($tabledForm, $_SESSION['shopper']);
			$orderLink = INDEX.'orderSent'.EXT.'orderID='.$serverRecord;
			$inquirer = getSender();
			
			$output = '
			<div class="container_12">
				<div class="grid_12 leftAlign top">
					<h2>Order Confirmation</h2>
					<p>
						Hi '.getSenderName().', <br />Thank you for shopping in Sole Service Manila! Below is the summary of your most recent order. Please follow the payment procedures for your order confirmation.
					</p>
					<p>
						'.$tabledForm.'
					</p>
				</div>
				<div class="clear">
				<!--spanner-->
				</div>
				<div class="grid_12">
				<p class="leftAlign">
						
						<b>Sender:</b> '.getSender().'<br />
						Here\'s the link to your pending order:<br /><br />
						<a href="'.$orderLink.'">'.$orderLink.'</a><br /><br />
						A copy of this link has been sent to your email for future reference. This is also where you should upload the photograph of your proof of purchase (deposit slip) to confirm your purchase.
					</p>
				</div>
				<div class="grid_2">
				&nbsp;
				</div>
			</div>
			';
			/*
//SEND EMAIL TO CLIENT
			sendThisEmail($tabledForm, $title, $success, $fail, $sendTo, $fromEmail, $verifyEmail);
			*/
			//SEND EMAIL TO ORDERSLIST
			echo sendThisEmail($tabledForm.'<br /><br />Order Link: <a href="'.$orderLink.'">'.$orderLink.'</a>', 'Order Inquiry: '.dateParser(explode('-', sqlDateNow())), '', '', 'default', $inquirer, true);
			
			echo sendThisEmail('We have successfully received your order. Here\'s the link to view the details:<br /><br /><a href="'.$orderLink.'">'.$orderLink.'</a><br /><br />This is an automated email service. Please do not reply to this email. Send your inquirires to inquiry@soleservice.com.ph instead. Thank you!', 'You have successfully placed an order at www.soleservice.com.ph', '', '', $inquirer, 'Sole Service Order Form <orders@soleservice.com.ph>', true);
			
			unset($_SESSION['cart']);
			return $output;
		}
		return '404 Not found.';
	}
?>